﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using KBManagement.DAL;
using KBManagement.Library;

namespace KBManagement
{
    public partial class ReportStore : Form
    {
        KBSQLClassDataContext context;
        public ReportStore(IQueryable storeQuery)
        {
            System.Data.DataTable myTable = new DataTable("myTable");
            System.Data.DataColumn column;
            System.Data.DataRow row;

            //create 3 columns
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ID";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "S_Name";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "Quantity";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Unit";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "QuantityStandard";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Warning";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);

            context = Utils.CreateInstance().GetDBContext();

            InitializeComponent();


            ReportDocument reportDocument = new ReportDocument();
            string path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            reportDocument.Load(path + "Report/StoreMainCrystalReport.rpt");

            foreach (Store store in storeQuery)
            {
                row = myTable.NewRow();
                row["ID"] = store.ID;
                row["S_Name"] = store.S_Name;
                row["Quantity"] = store.Quantity;
                row["Unit"] = store.Unit;
                row["QuantityStandard"] = store.QuantityStandard;
                row["Warning"] = store.warning;
                //add the row of data to the table
                myTable.Rows.Add(row);
            }
            reportDocument.SetDataSource(myTable);
            crystalReportViewer1.ReportSource = reportDocument;
        }
    }
}
